package com.mlog.njyjy.ndop.share.sql.config;

import com.jfinal.plugin.hikaricp.HikariCpPlugin;
import com.mlog.njyjy.ndop.share.sql.service.SqlPopulateService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;


/**
 * <p>
 * .
 * </p>
 *
 * @author <a href="mailto:yingxiufeng@mlogcn.com">alex</a>
 * @version v1.0, 2020/7/8
 */
@Configuration
@EnableConfigurationProperties({SqlDataSourceProperties.class})
public class SqlOrmConfiguration {

    private final SqlDataSourceProperties properties;

    public SqlOrmConfiguration(SqlDataSourceProperties properties) {
        this.properties = properties;
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    @Primary
    public HikariCpPlugin hikariCpPlugin() {
        return new HikariCpPlugin(properties.getDataSource().getUrl(), properties.getDataSource().getUsername(),
                properties.getDataSource().getPassword(), properties.getDataSource().getDriverClassName());
    }

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @Order(1989)
    @Primary
    public SqlPopulateService sqlPopulateService() {
        return new SqlPopulateService(properties, hikariCpPlugin());
    }


}
